Skip to content

8356165: System.in in jshell replace supplementary characters with ?? #25079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented May 7, 2025

When reading from System.in in a JShell snippet, JShell first reads the whole line (getting a String), and then converts this characters from this String to bytes on demand. But, it does not convert multi-surrogate code points correctly, it tries to convert each surrogate separately, which cannot work.

The proposal herein is to, when the current character is a high surrogate, peek at the next character, and if it is a low surrogate, convert both the high and low surrogates to bytes together.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8356165: System.in in jshell replace supplementary characters with ?? (Bug - P3)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/25079/head:pull/25079
$ git checkout pull/25079

Update a local copy of the PR:
$ git checkout pull/25079
$ git pull https://git.openjdk.org/jdk.git pull/25079/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25079

View PR using the GUI difftool:
$ git pr show -t 25079

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/25079.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 7, 2025

👋 Welcome back jlahoda! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented May 7, 2025

@lahodaj This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8356165: System.in in jshell replace supplementary characters with ??

Reviewed-by: cstein, asotona

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 241 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the rfr Pull request is ready for review label May 7, 2025
@openjdk
Copy link

openjdk bot commented May 7, 2025

@lahodaj The following label will be automatically applied to this pull request:

  • kulla

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented May 7, 2025

Webrevs

@@ -977,7 +977,15 @@ public void perform(LineReaderImpl in) throws IOException {
public synchronized int readUserInput() throws IOException {
if (pendingBytes == null || pendingBytes.length <= pendingBytesPointer) {
char userChar = readUserInputChar();
pendingBytes = String.valueOf(userChar).getBytes();
StringBuilder dataToConvert = new StringBuilder();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, add here the comment from the PR description for readers from the future:

[...] when the current character is a high surrogate, peek at the next character, and if it is a low surrogate, convert both the high and low surrogates to bytes together.

The (internal) API used in the implementation doesn't express that on first sight.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding comments.

Comment on lines 983 to 986
if (pendingLine.length() > pendingLinePointer &&
Character.isLowSurrogate(pendingLine.charAt(pendingLinePointer))) {
dataToConvert.append(readUserInputChar());
}
Copy link
Contributor

@tats-u tats-u May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about combining readUserInputChar() and (only when not surrogate pair but just isolated code unit) pendingLinePointer--?

pendingLinePointer-- will be unlikely to be happen for normal inputs other than penetration tests.

Comment on lines 52 to 53
inputSink.write("new String(System.in.readNBytes(4))\n\uD83D\uDE03\n");
waitOutput(out, "\"\uD83D\uDE03\"");
Copy link
Contributor

@tats-u tats-u May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the following is robuster:

-            inputSink.write("new String(System.in.readNBytes(4))\n\uD83D\uDE03\n");
-            waitOutput(out, "\"\uD83D\uDE03\"");
+            inputSink.write("new String(System.in.readNBytes(5))\n\uD83D\uDE031\n");
+            waitOutput(out, "\"\uD83D\uDE031\"");

@tats-u
Copy link
Contributor

tats-u commented May 12, 2025

I forgot to explain the context:

  • Increment of the number of input bytes to be read is to assure that a byte or code unit is not read twice or skipped
  • Providing extra input is to prevent the suspension due to the input starvation (especially for builds without this fix)

@lahodaj
Copy link
Contributor Author

lahodaj commented May 13, 2025

I forgot to explain the context:

* Increment of the number of input bytes to be read is to assure that a byte or code unit is not read twice or skipped

* Providing extra input is to prevent  the suspension due to the input starvation (especially for builds without this fix)

I missed the additional 1 there. But, I think the current code, although more complex, satisfies the constraints as well (we are reading the end-of-line, and checking for it).

@tats-u
Copy link
Contributor

tats-u commented May 13, 2025

I will believe you have considered the difference of the length of EOL in Windows and Unix.

@lahodaj
Copy link
Contributor Author

lahodaj commented May 19, 2025

I will believe you have considered the difference of the length of EOL in Windows and Unix.

Yes, the test handles both Unix and Windows EOL (that's the complicating factor).

@tats-u
Copy link
Contributor

tats-u commented May 19, 2025

I suspect I've been misunderstood the argument for readNBytes in the test case. Sorry to have bothered you.

Copy link
Member

@sormuras sormuras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Member

@asotona asotona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 19, 2025
@@ -977,7 +977,20 @@ public void perform(LineReaderImpl in) throws IOException {
public synchronized int readUserInput() throws IOException {
if (pendingBytes == null || pendingBytes.length <= pendingBytesPointer) {
char userChar = readUserInputChar();
pendingBytes = String.valueOf(userChar).getBytes();
StringBuilder dataToConvert = new StringBuilder();
Copy link
Contributor

@tats-u tats-u May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I think we can avoid using StringBuilder (and make the code more RAM-friendly):

char[] dataToConvert = { useChar, '\0' };
// if (...) {
// ...
// if (...) {
// ...
dataToConvert[1] = lowSurrogate;
// }
// ...
// }
// low-surrogate code unit never be null char
pendingBytes = dataToConvert[1] != '\0' ? String.valueOf(dataToConvert) : String.valueOf(dataToConvert[0]);

The next version of .NET is said to be able to allocate such a tiny array to the stack, instead of the heap, but I don't know whether JVM can do the same optimization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do something like that, although I wrote it as I wrote it mostly because that's more clearly correct. Although the current tests probably cover all the cases, so with a bit of work, we probably could eliminate the (explicit) array completely.

Overall, on most places, it is usually not necessary to be too clever - the VM can optimize and eliminate allocations if needed.

I'll leave it up to Adam and/or Christian whether they would prefer a slightly more complex code with less (explicit/visible) allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, we could do this:
lahodaj@6a07648

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability of the code is my preference unless the performance is absolutely critical (not this case).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lahodaj@6a07648

Better than my suggestion.

The current code using StringBuilder is not bad because the act to pass a very long string to JShell seems to be something like shooting ourselves in the foot.

Copy link
Contributor

@tats-u tats-u May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can /integrate the current code with StringBuilder.

@lahodaj
Copy link
Contributor Author

lahodaj commented May 20, 2025

/integrate

@openjdk
Copy link

openjdk bot commented May 20, 2025

Going to push as commit e961b13.
Since your change was applied there have been 250 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 20, 2025
@openjdk openjdk bot closed this May 20, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 20, 2025
@openjdk
Copy link

openjdk bot commented May 20, 2025

@lahodaj Pushed as commit e961b13.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated kulla [email protected]
Development

Successfully merging this pull request may close these issues.

4 participants